package com.sky.controller.appUser;



import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.AppUserDTO;
import com.sky.dto.AppUserLoginDTO;
import com.sky.entity.AddressBook;
import com.sky.entity.AppUser;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.AppUserVO;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController("appUserController")
@RequestMapping("/app/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private JwtProperties jwtProperties;
//    @GetMapping
//    public void register(String name, String password, HttpServletResponse response) throws IOException {
//        System.out.println(name);
//        System.out.println(password);
//        boolean flag=userService.addUser(name,password);
//        String Result="T";
//        if (!flag){
//            Result="F";
//            log.info("注册失败");
//        }
//        log.info("注册用户名：{}",name);
//        DataOutputStream dos=new DataOutputStream(response.getOutputStream());
//        dos.writeUTF(Result);
//    }

    @Operation(summary = "用户注册", description = "用户注册")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "注册成功")
    )
    @PostMapping("/register")
    //TODO 添加jwt令牌
    //TODO 添加头像
    public Result register(@RequestBody AppUserDTO appUserDTO) throws IOException {
        log.info("注册用户：{}",appUserDTO);
        boolean flag=userService.addAppUser(appUserDTO);
//        DataOutputStream dos=new DataOutputStream(response.getOutputStream());
//        dos.writeUTF(Result);
        return Result.success();
    }
    @Operation(summary = "用户登录", description = "用户登录")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "登录成功")
    )
    @PostMapping("/login")
    public Result login(@RequestBody AppUserLoginDTO appUserLoginDTO) throws IOException {
        log.info("用户登录：{}",appUserLoginDTO);
        AppUser appUser=userService.appLogin(appUserLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.APP_USER_ID, appUser.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAppSecretKey(),
                jwtProperties.getAppTtl(),
                claims);
        log.info("用户登录成功，用户信息为：{}",appUser);
        AppUserVO appUserVO  = AppUserVO.builder()
                .id(appUser.getId())
                .username(appUser.getUsername())
                .nick(appUser.getNick())
                .token(token)
                .build();
        log.info("用户登录成功，用户信息为：{}",appUserVO);
        return Result.success(appUserVO);
    }


    @Operation(summary = "根据ID查询用户", description = "根据ID查询用户")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "根据ID成功查询用户")
    })
    @GetMapping("/info/{id}")
    public Result<AppUser> getAppUserById(@PathVariable Long id){
        log.info("根据ID查询用户：{}", id);
        AppUser appUser = userService.getById(id);
        return Result.success(appUser);
    }

    @PutMapping("/update")
    public Result updateAppUser(@RequestBody AppUser appUser){
        log.info("修改用户信息：{}", appUser);
        userService.update(appUser);
        return Result.success();
    }

    @Operation(summary = "根据userID查询地址", description = "根据userID查询地址")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "根据userID成功查询地址")
    })
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("根据userID查询地址：{}", id);
        AddressBook addressBook = addressBookService.getByUserId(id);
        return Result.success(addressBook);
    }

}
